package org.zhadoop.stream.cql.tasks;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zhadoop.stream.cql.DriverContext;
import org.zhadoop.stream.cql.exception.CQLException;
import org.zhadoop.stream.cql.hooks.SemanticAnalyzeHook;
import org.zhadoop.stream.cql.semanticanalyzer.parser.context.ParseContext;
import org.zhadoop.streaming.config.StreamingConfig;
import org.zhadoop.streaming.exception.ErrorCode;

/**
 * 创建各类执行任务的工厂类
 * @author zwd
 *
 */
public class TaskFactory {
	private static final Logger LOG = LoggerFactory.getLogger(TaskFactory.class);
	
	/**
	 * 创建task实例
	 * @param driverContext
	 * @param parseContext
	 * @param config
	 * @param analyzeHooks
	 * @return
	 * @throws CQLException 
	 */
	public static Task createTask(DriverContext driverContext,ParseContext parseContext,
			StreamingConfig config,List<SemanticAnalyzeHook> analyzeHooks) throws CQLException{
		Task task = parseContext.createTask(driverContext, analyzeHooks);
		if(task == null){
			LOG.error("Can not find task excutor for parse context '{}'.", parseContext.getClass().getName());
			CQLException exception = new CQLException(ErrorCode.UNKNOWN_SERVER_COMMON_ERROR);
			throw exception;
		}
		task.init(driverContext, config, analyzeHooks);
		return task;
	}
	
}
